<!--
 * @Description: 机票行程信息管理
 * @Version: 2.0
 * @Autor: 许国辉
 * @Date: 2022-10-26 08:21:01
 * @LastEditors: 许国辉
 * @LastEditTime: 2023-05-15 16:31:39
-->
<template>
  <div>
    <PageWrapper>
      <a-tabs v-model:activeKey="activeKey" @change="tabChange">
        <a-tab-pane key="1" tab="普通订单" />
        <a-tab-pane key="2" tab="改期订单" />
        <a-tab-pane key="3" tab="退票订单" />
      </a-tabs>
    </PageWrapper>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button
          type="primary"
          @click="customHeader"
          v-if="activeKey === '1' || activeKey === '2'"
        >
          导出表格
        </a-button>
        <a-button type="primary" @click="customHeader1" v-if="activeKey === '3'">
          导出表格
        </a-button>
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref, toRaw } from 'vue';
  import {
    getFlightTicketOrderByParams,
    getFlightTicketOrderByParams1,
    getFlightTicketOrderByParams2,
  } from '/@/api/travelItinerary/airItineraryAPI';
  import { Tabs } from 'ant-design-vue';
  import { PageWrapper } from '/@/components/Page';
  import { BasicTable, useTable } from '/@/components/Table';
  import { columns, columns1, searchFormSchema } from './airItineraryData';
  import { jsonToSheetXlsx } from '/@/components/Excel';
  import { OrderAir } from '/@/api/travelItinerary/model/airItineraryModel';
  import dayjs from 'dayjs';
  export default defineComponent({
    components: {
      [Tabs.name]: Tabs,
      [Tabs.TabPane.name]: Tabs.TabPane,
      PageWrapper,
      BasicTable,
    },
    setup() {
      const orderAir = ref<OrderAir>();
      const visible = ref<boolean>(false);
      let api = ref(getFlightTicketOrderByParams);
      let title = ref('普通订单');
      const [registerTable, { reload, getDataSource, setColumns, getForm }] = useTable({
        api,
        pagination: { pageSize: 10 },
        columns: columns,
        formConfig: {
          schemas: searchFormSchema,
          fieldMapToTime: [['fieldTime', ['deptDateStart', 'deptDateEnd'], 'YYYY-MM-DD']],
        },
        showTableSetting: true,
        tableSetting: { fullScreen: true },
        striped: false,
        useSearchForm: true,
        bordered: true,
        showIndexColumn: false,
      });
      // console.log(getColumns());
      // console.log(getAirOrderByParams);

      function send(record: OrderAir) {
        orderAir.value = toRaw(record);
        visible.value = true;
        // airItineraryInfo(true, { ...record });
      }

      function handleSuccess() {
        reload();
      }
      async function tabChange(activeKey) {
        if (activeKey === '1') {
          setColumns(columns);
          title.value = '普通订单';
          api.value = getFlightTicketOrderByParams;
          await getForm().submit;
          reload();
        } else if (activeKey === '2') {
          setColumns(columns);
          title.value = '改期订单';
          api.value = getFlightTicketOrderByParams1;
          // console.log(getForm());
          await getForm().submit;

          reload();
        } else {
          setColumns(columns1);
          title.value = '退票订单';
          api.value = getFlightTicketOrderByParams2;
          // console.log(getForm());
          await getForm().submit;
          reload();
        }
      }
      const hideModal = () => {
        visible.value = false;
      };
      function customHeader() {
        let header1 = {
          airOrderId: '订单号',
          reservedDate: '预约时间',
          bookerName: '预定人',
          passengerName: '乘机人',
          orgname: '出发地',
          dstname: '目的地',
          deptDate: '起飞日期',
          arriveDate: '到达日期',
          travelType: '预约类型',
          orderStatus: '状态',
          sumPrice: '总金额',
        };
        interface Orderdata {
          airOrderId: string;
          reservedDate: string;
          bookerName: string;
          passengerName: string;
          orgname: string;
          dstname: string;
          deptDate: string;
          arriveDate: string;
          travelType: string;
          orderStatus: string;
          sumPrice: string;
        }
        let orderArr: Orderdata[] = [];
        for (let k = 0; k < toRaw(getDataSource()).length; k++) {
          let orderdata = ref<Orderdata>({
            airOrderId: '订单号',
            reservedDate: '预约时间',
            bookerName: '预定人',
            passengerName: '乘机人',
            orgname: '出发地',
            dstname: '目的地',
            deptDate: '起飞日期',
            arriveDate: '到达日期',
            travelType: '预约类型',
            orderStatus: '状态',
            sumPrice: '总金额',
          });
          orderdata.value!.airOrderId = toRaw(getDataSource())[k].airOrderId;
          orderdata.value!.reservedDate = toRaw(getDataSource())[k].reservedDate;
          orderdata.value!.bookerName = toRaw(getDataSource())[k].bookerName;
          orderdata.value!.passengerName = toRaw(getDataSource())[k].passengerName;
          orderdata.value!.orgname = toRaw(getDataSource())[k].orgname;
          orderdata.value!.dstname = toRaw(getDataSource())[k].dstname;
          orderdata.value!.deptDate = toRaw(getDataSource())[k].deptDate;
          orderdata.value!.arriveDate = toRaw(getDataSource())[k].arriveDate;
          if (toRaw(getDataSource())[k].travelType === 1) {
            orderdata.value!.travelType = '有审批单';
          } else {
            orderdata.value!.travelType = '无审批单';
          }
          if (toRaw(getDataSource())[k].orderStatus === 0) {
            orderdata.value!.orderStatus = '无效';
          } else if (toRaw(getDataSource())[k].orderStatus === 1) {
            orderdata.value!.orderStatus = '原始';
          } else {
            orderdata.value!.orderStatus = '已出票';
          }
          orderdata.value!.sumPrice = toRaw(getDataSource())[k].sumPrice;
          orderArr.push(orderdata.value);
        }
        jsonToSheetXlsx({
          data: orderArr,
          header: header1,
          filename: '机票' + title.value + '.xlsx',
          json2sheetOpts: {
            // 指定顺序
            // header: ['name', 'id'],
          },
        });
      }
      function customHeader1() {
        let header = {
          airOrderId: '订单号',
          returnDate: '退票时间',
          returnOldOrderId: '原订单号',
          returnLinkMan: '申请人',
          passengerName: '乘机人',
          orgname: '出发地',
          dstname: '目的地',
          quitReason: '退票性质',
          quitIsRuse: '退票状态',
          travelType: '预约类型',
          sumPrice: '总金额',
        };
        interface Orderdata {
          airOrderId: string;
          returnDate: string;
          returnOldOrderId: string;
          returnLinkMan: string;
          passengerName: string;
          orgname: string;
          dstname: string;
          quitReason: string;
          quitIsRuse: string;
          travelType: string;
          sumPrice: string;
        }
        let orderArr: Orderdata[] = [];
        for (let k = 0; k < toRaw(getDataSource()).length; k++) {
          let orderdata = ref<Orderdata>({
            airOrderId: '订单号',
            returnDate: '退票时间',
            returnOldOrderId: '原订单号',
            returnLinkMan: '申请人',
            passengerName: '乘机人',
            orgname: '出发地',
            dstname: '目的地',
            quitReason: '退票性质',
            quitIsRuse: '退票状态',
            travelType: '预约类型',
            sumPrice: '总金额',
          });
          orderdata.value!.airOrderId = toRaw(getDataSource())[k].airOrderId;
          orderdata.value!.returnDate = toRaw(getDataSource())[k].returnDate;
          orderdata.value!.returnOldOrderId = toRaw(getDataSource())[k].returnOldOrderId;
          orderdata.value!.returnLinkMan = toRaw(getDataSource())[k].returnLinkMan;
          orderdata.value!.passengerName = toRaw(getDataSource())[k].passengerName;
          orderdata.value!.orgname = toRaw(getDataSource())[k].orgname;
          orderdata.value!.dstname = toRaw(getDataSource())[k].dstname;
          orderdata.value!.quitReason = toRaw(getDataSource())[k].quitReason;
          orderdata.value!.quitIsRuse = toRaw(getDataSource())[k].quitIsRuse;
          orderdata.value!.travelType = toRaw(getDataSource())[k].travelType;
          orderdata.value!.sumPrice = toRaw(getDataSource())[k].sumPrice;
          orderArr.push(orderdata.value);
        }
        jsonToSheetXlsx({
          data: orderArr,
          header: header,
          filename: '机票' + title.value + dayjs(new Date()).format('YYYY- MM - DD') + '.xlsx',
          json2sheetOpts: {
            // 指定顺序
            // header: ['name', 'id'],
          },
        });
      }
      onMounted(() => {
        //employeeData.initEmployeeData();
      });

      return {
        activeKey: ref('1'),
        tabChange,
        registerTable,
        handleSuccess,
        hideModal,
        send,
        customHeader,
        customHeader1,
        visible,
        orderAir,
      };
    },
  });
</script>
